package com.exchange.aspect.user;

import com.exchange.user.entity.SysUser;
import com.exchange.user.service.SysUserService;
import com.exchange.utils.result.CommonResult;
import com.exchange.utils.result.ResultCodeEnum;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

/***
 *                    .::::.
 *                  .::::::::.
 *                 :::::::::::
 *             ..:::::::::::'
 *           '::::::::::::'
 *             .::::::::::
 *        '::::::::::::::..
 *             ..::::::::::::.
 *           ``::::::::::::::::
 *            ::::``:::::::::'        .:::.
 *           ::::'   ':::::'       .::::::::.
 *         .::::'      ::::     .:::::::'::::.
 *        .:::'       :::::  .:::::::::' ':::::.
 *       .::'        :::::.:::::::::'      ':::::.
 *      .::'         ::::::::::::::'         ``::::.
 *  ...:::           ::::::::::::'              ``::.
 * ```` ':.          ':::::::::'                  ::::..
 *                    '.:::::'                    ':'````..
 *
 * @Author liuhai
 * @Date 2020-10-04 15:33:45
 * @Description 校验用户登陆切面
 */
@Configuration
@Aspect
public class CheckLoginAspect {

    @Resource
    private SysUserService sysUserService;

    @Pointcut("@annotation(com.exchange.aspect.user.CheckLogin)")
    public void pointcut() {    }

    @Around(value = "pointcut()")
    public CommonResult checkLogin(ProceedingJoinPoint joinPoint) throws Exception{
        try {
            Signature signature = joinPoint.getSignature();
            if (!(signature instanceof MethodSignature)) {
                throw new IllegalArgumentException("该注解只能用于方法");
            }
            // 用户token
            String userToken = "";
            HttpServletRequest request = null;

            ServletRequestAttributes attributes =(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            request = attributes.getRequest();
            Enumeration<String> headNames = request.getHeaderNames();
            String headName = null;
            while(headNames.hasMoreElements()){
                headName = headNames.nextElement();
                if("user_token".equals(headName)){
                    userToken = request.getHeader(headName);
                }
            }

            SysUser sysUser = null;
            if(!"".equals(userToken)){
                sysUser = sysUserService.getUserByToken(userToken);
            }
            CommonResult proceed = null;
            if(null == sysUser){
                // 用户信息为空，拦截
                proceed = new CommonResult();
                proceed.setCode(ResultCodeEnum.NOLOGIN.getCode());
                proceed.setMessage("user_token无效");
            }else {
                // 可以获取到用户信息，放行
                proceed = (CommonResult)joinPoint.proceed();
            }
            return proceed;
        } catch (Throwable throwable) {
            throw new Exception(throwable);
        }
    }


}
